package com.mdd.admin.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.system.ISystemDevPayWayService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.system.SystemDevPayWayParam;
import com.mdd.admin.validate.system.SystemDevPayWayUpdateParam;
import com.mdd.admin.vo.course.CourseListVo;
import com.mdd.admin.vo.system.SystemDevPayWayListVo;
import com.mdd.admin.vo.system.SystemDevPayWayDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.course.Course;
import com.mdd.common.entity.system.SystemDevPayWay;
import com.mdd.common.mapper.system.SystemDevPayWayMapper;
import com.mdd.common.utils.ArrayUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.common.config.GlobalConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 支付方式实现类
 * @author panweiliang
 */
@Service
public class SystemDevPayWayServiceImpl implements ISystemDevPayWayService {
        
    @Resource
    SystemDevPayWayMapper systemDevPayWayMapper;

    /**
     * 支付方式列表
     *
     * @author panweiliang
     * @return List<SystemDevPayWayListVo>
     */
    @Override
    public Map<String, List<SystemDevPayWayListVo>> list() {

        MPJQueryWrapper<SystemDevPayWay> mpjQueryWrapper = new MPJQueryWrapper<SystemDevPayWay>()
                .selectAll(SystemDevPayWay.class)
                .select("pay.name as payName, pay.image as payImage, pay.pay_way as payWay")
                .innerJoin("?_dev_pay pay ON pay.id=t.pay_id".replace("?_", GlobalConfig.knowledgePaymentTablePrefixe))
                .orderByDesc(Arrays.asList("t.id"));
        List<SystemDevPayWayListVo> list = systemDevPayWayMapper.selectJoinList(SystemDevPayWayListVo.class, mpjQueryWrapper);

        Map<String, List<SystemDevPayWayListVo>> resultMap = new LinkedHashMap<>();

        List<SystemDevPayWayListVo> minprogram = new LinkedList<>();
        List<SystemDevPayWayListVo> officialAccounts = new LinkedList<>();
        List<SystemDevPayWayListVo> h5 = new LinkedList<>();
        List<SystemDevPayWayListVo> other = new LinkedList<>();

        //场景:1-微信小程序;2-微信公众号;3-H5;4-PC商城;5-APP;
        list.forEach(item -> {
            item.setPayImage(UrlUtil.toAbsoluteUrl(item.getPayImage()));
            Integer scene = item.getScene();
            switch (scene) {
                case 1:
                    minprogram.add(item);
                    break;
                case 2:
                    officialAccounts.add(item);
                    break;
                case 3:
                    h5.add(item);
                    break;
                default:
                    other.add(item);
                    break;
            }
        });

        resultMap.put("minprogram", minprogram);
        resultMap.put("officialAccounts", officialAccounts);
        resultMap.put("h5", h5);
        resultMap.put("other", other);
        return resultMap;
    }

    @Override
    public void update(SystemDevPayWayUpdateParam systemDevPayWayUpdateParam) {
        List<SystemDevPayWayParam> minprogram = systemDevPayWayUpdateParam.getMinprogram();
        List<SystemDevPayWayParam> officialAccounts = systemDevPayWayUpdateParam.getOfficialAccounts();
        List<SystemDevPayWayParam> h5 = systemDevPayWayUpdateParam.getH5();
        if (minprogram != null) {
            minprogram.forEach(item -> {
                SystemDevPayWay devPayWay = new SystemDevPayWay();
                BeanUtils.copyProperties(item, devPayWay);
                this.systemDevPayWayMapper.updateById(devPayWay);
            });
        }
        if (officialAccounts != null) {
            officialAccounts.forEach(item -> {
                SystemDevPayWay devPayWay = new SystemDevPayWay();
                BeanUtils.copyProperties(item, devPayWay);
                this.systemDevPayWayMapper.updateById(devPayWay);
            });
        }
        if (h5 != null) {
            h5.forEach(item -> {
                SystemDevPayWay devPayWay = new SystemDevPayWay();
                BeanUtils.copyProperties(item, devPayWay);
                this.systemDevPayWayMapper.updateById(devPayWay);
            });
        }
    }

    /**
     * 支付方式状态
     * @param id 支付方式主键
     */
    @Override
    public SystemDevPayWay changeStatus(Integer id) {
        SystemDevPayWay systemDevPayWay = systemDevPayWayMapper.selectOne(
                new QueryWrapper<SystemDevPayWay>()
                        .eq("id", id)
                        .last("limit 1"));
        Assert.notNull(systemDevPayWay, "支付方式不存在!");
        systemDevPayWay.setStatus(systemDevPayWay.getStatus() == 1 ? 0 : 1);
        systemDevPayWayMapper.updateById(systemDevPayWay);
        return systemDevPayWay;
    }

    /**
     * 设置为默认支付方式
     *
     * @param id 支付方式主键
     */
    @Override
    public SystemDevPayWay changeAsDefault(Integer id) {
        SystemDevPayWay systemDevPayWay = systemDevPayWayMapper.selectOne(
                new QueryWrapper<SystemDevPayWay>()
                        .eq("id", id)
                        .last("limit 1"));
        Assert.notNull(systemDevPayWay, "支付方式不存在!");
        List<SystemDevPayWay> list = systemDevPayWayMapper.selectList(new QueryWrapper<SystemDevPayWay>()
                .eq("scene", systemDevPayWay.getScene()));
        for (SystemDevPayWay devPayWay : list) {
            devPayWay.setIsDefault(0);

            this.systemDevPayWayMapper.updateById(devPayWay);
        }
        systemDevPayWay.setIsDefault(1);
        systemDevPayWayMapper.updateById(systemDevPayWay);
        return systemDevPayWay;
    }
}
